package com.yangjun.common.timetask;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.dao.mybatis.mapper.realtimeLiveAnalysis.flowAnalysisMapper;
import com.yangjun.dao.mybatis.mapper.yinong.YiNongMapper;
import com.yangjun.entity.yinong.YiNongOrderEntity;
import com.yangjun.entity.yinong.YiNongOrderLogEntity;
import com.yangjun.service.MailService;
import com.yangjun.service.report.SingleProductReprotExportService;
import com.yangjun.service.report.TMallRxReprotExportService;
import com.yangjun.service.report.TargetMemberReprotExportService;
import com.yangjun.utils.DvDateUtils;
import com.yangjun.utils.HttpUtils;

import cn.hutool.http.HttpRequest;

@Component
public class ScheduledTasks {
	private static Logger logger = Logger.getLogger(ScheduledTasks.class);  

	@Autowired
    private flowAnalysisMapper flowAnalysisMapper;
	@Autowired
	private SingleProductReprotExportService singleProductReprotExportService;
	@Autowired
	private TMallRxReprotExportService tMallRxReprotExportService;
	@Autowired
	private TargetMemberReprotExportService targetMemberReprotExportService;
	@Autowired
	private MailService mailService;
	@Autowired
	private YiNongMapper yiNongMapper;
	/** 
	 * @Description 将搜索结果为空的关键词定时插入到DV_SEARCH_KEYWORD_NULLRESULT表中
	 * @throws InterruptedException
	 * @author yangjun
	 * @Date 2016年10月14日 上午10:32:12
	 */
	@Scheduled(cron = "0 */10 * * * *")
    public void insertSerchKywrodByNullResult() throws InterruptedException {
		List<Map<String,String>> nullResutList = Lists.newArrayList();
		Map<String,String> map = null;
		//查询出没有点击的过的并且搜索人数多的关键词
    	List<Map<String,Object>> ResutList = flowAnalysisMapper.querySearchKeywordNullResult();
    	if(ResutList.size()>0){
    		//每个关键词进行http请求调用接口（研发其实需要提供接口）
        	List<Map<String,String>> searchList= HttpUtils.threadGet(ResutList);
        	for(Map<String,String> searchMap: searchList){
        		//获取搜索结果，搜索结果为0的需要插入数据库
        		if(searchMap!=null && StringUtils.isNotBlank(searchMap.get("results")) && Integer.parseInt(searchMap.get("results").toString())==0){
        			map = Maps.newHashMap();
        			map.put("siteId",searchMap.get("siteId").toString());
        			map.put("searchkeyword",searchMap.get("searchkeyword").toString());
        			nullResutList.add(map);
        		}
        	}
        	//先清空当天的表再插入，mybatis用批量插入，参数是List<Map<String,String>>
        	flowAnalysisMapper.deleteSearchKeywordByNullResult();
        	if(nullResutList!=null&&nullResutList.size()>0){
            	flowAnalysisMapper.insertSearchKeywordByNullResult(nullResutList);
        	}
    	}
	}
	@Scheduled(cron = "0 30 08 * * *")
    public void updateDayReport(){
		//更新单品分析报表
		singleProductReprotExportService.updateReport();
		//更新目标会员报表
		targetMemberReprotExportService.updateReport();
    }
	@Scheduled(cron = "0 30 * * * *")
    public void updateHalfHourReport(){
		//更新天猫Rx单分析报表
		tMallRxReprotExportService.updateReport();
    }
	@Scheduled(cron = "0 30 08 * * *")
    public void sendEmail(){
		String yesterday = DvDateUtils.getDate(new Date(),-1);
		//钟芷媚
//		mailService.sendAppPageTypeFlowReport(yesterday);
		//邝玲玲
		mailService.sendSEMSourceKeywordReport("day",yesterday,yesterday,true);
		mailService.sendAppSalesReport(yesterday);
		mailService.sendWebSalesReport(yesterday);
		//杨燕玲
		mailService.sendSEOSourceKeywordReport("day",yesterday,yesterday,true);
		mailService.sendSeoProductFlowReport(yesterday);
		mailService.sendSeoProductSalesReport(yesterday);
		mailService.sendSeoChannelFlowSalesReport(yesterday);
		//陈君文
//		mailService.sendWebSearchKeywordZiXunReport(yesterday);
//		mailService.sendKzoneReport(yesterday);
//		mailService.sendKzoneGoodsReport(yesterday);
//		mailService.sendWebSearchKeywordReport(yesterday);
//		mailService.sendWebSearchKeywordGoodsReport(yesterday);
//		mailService.sendAppSearchKeywordReport(yesterday);
		//陈德烨
//		mailService.sendWebKzoneReport(yesterday);

    }
	@Scheduled(cron = "0 30 08 * * *")
	public void pushYiNongOrder(){
		String yesterday = DvDateUtils.getDate(new Date(),-1);
		List<YiNongOrderEntity> yiNongOrderList = yiNongMapper.queryYiNongOrderByDate(yesterday);
		if(yiNongOrderList!=null&&yiNongOrderList.size()>0){
			for(YiNongOrderEntity yiNongOrder : yiNongOrderList){
				YiNongOrderLogEntity yiNongOrderLog = new YiNongOrderLogEntity();
				yiNongOrderLog.setCreatedate(yiNongOrder.getCreatedate());
				yiNongOrderLog.setIp(yiNongOrder.getIp());
				yiNongOrderLog.setNetamt( yiNongOrder.getNetamt());
				yiNongOrderLog.setOrdercode(yiNongOrder.getOrdercode());
				yiNongOrderLog.setPaymentdate(yiNongOrder.getPaymentdate());
				yiNongOrderLog.setSubmit(false);
				yiNongMapper.insertYiNongOrderLog(yiNongOrderLog);
				
				HashMap<String, String> paramMap = new HashMap<String, String>();
				paramMap.put("phone", null);
				paramMap.put("ip_address", yiNongOrder.getIp().toString());
				paramMap.put("order_time", yiNongOrder.getCreatedate().toString());
				paramMap.put("order_amount", yiNongOrder.getNetamt().toString());
				paramMap.put("spmc",null);
				paramMap.put("payment_time", yiNongOrder.getPaymentdate().toString());
				paramMap.put("ordernumber", yiNongOrder.getOrdercode());
				paramMap.put("type", "7");
				String result= HttpRequest.post("http://183.63.135.214:9092/portal2/saveOrder").body(JSON.toJSONString(paramMap))
					    .execute().body();
				System.out.println(result);
				if(JSONObject.parseObject(result).getInteger("success")==0){
					System.out.println(yiNongOrder.getOrdercode()+"保存成功");
					yiNongOrderLog.setSubmit(true);
					yiNongMapper.updateYiNongOrderLog(yiNongOrderLog);
				}
			}
		}
	}
	
}
